package com.heimali.sf.calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.TimePicker;

import com.heimali.sf.R;
import com.heimali.sf.db.NoteHelper;
import com.heimali.sf.models.Note;
import com.heimali.sf.utils.AndroidUtils;
import com.heimali.sf.utils.SystemUtils;

public class AddNoteActivity extends Activity implements OnClickListener{
	
	private EditText noteNameEdit;
	private EditText noteRemarkEdit;
	private EditText noteTimeDateEdit;
	private EditText noteTimeMinuteEdit;
	private Button saveBtn;
	private Button cancelBtn;
	
	private int year = -1;
	private int month = -1;
	private int day = -1;
	private int hour = -1;
	private int minute = -1;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.note_add);
		initView();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	
	public void initView() {
		
		/* title bar */
		ImageButton leftButton = (ImageButton) findViewById(R.id.left_btn);
		leftButton.setOnClickListener(this);
		((TextView) findViewById(R.id.title_text)).setText(R.string.add_note);
		
		noteNameEdit = (EditText) findViewById(R.id.note_name);
		noteRemarkEdit = (EditText) findViewById(R.id.note_remark);
		noteTimeDateEdit = (EditText) findViewById(R.id.note_time_date);
		
		//noteTimeDateEdit.setText(SystemUtils.getCurrentDate());
		noteTimeDateEdit.setOnClickListener(this);
		noteTimeMinuteEdit = (EditText) findViewById(R.id.note_time_minute);
		//noteTimeMinuteEdit.setText(SystemUtils.getCurrentMinuteTime());
		noteTimeMinuteEdit.setOnClickListener(this);
		
		updateDateEditText();
		updateTiemEidtText();
		
		saveBtn = (Button) findViewById(R.id.note_save_btn);
		saveBtn.setOnClickListener(this);
		cancelBtn = (Button) findViewById(R.id.note_cancel_btn);
		cancelBtn.setOnClickListener(this);
	}
	
	public void updateDateEditText() {
		year = year < 0 ? SystemUtils.getCurrentYear() : year;
		month = month < 0 ? SystemUtils.getCurrentMonth() : month;
		day = day < 0 ? SystemUtils.getCurrentDay() : day;
		String display = year+"-";
		if (String.valueOf(month).length() == 1) {
			display += "0"+month;
		}else {
			display += month;
		}
		display += "-";
		if (String.valueOf(day).length() == 1) {
			display += "0"+day;
		}else {
			display += day;
		}
		noteTimeDateEdit.setText(display);
	}
	
	public void updateTiemEidtText() {
		hour = hour < 0 ? SystemUtils.getCurrentHour()+1 : hour;
		minute = minute  < 0 ? 0 : minute;
		String display = "";
		if (String.valueOf(hour).length() == 1) {
			display += "0"+hour;
		}else {
			display += hour;
		}
		display += ":";
		if (String.valueOf(minute).length() == 1) {
			display += "0"+minute; 
		}else {
			display += minute;
		}
		noteTimeMinuteEdit.setText(display);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.left_btn:
			finish();
			break;
		case R.id.note_cancel_btn:
			finish();
			break;
		case R.id.note_save_btn:
			saveBtnClick();
			break;
		case R.id.note_time_date:
			displayDatePicker();
			break;
		case R.id.note_time_minute:
			displayTimePicker();
			break;
		}
	}
	
	private void saveBtnClick() {
		String name = noteNameEdit.getText().toString().trim();
		if (TextUtils.isEmpty(name)) {
			AndroidUtils.toast(this, R.string.note_name_error);
			return ;
		}
		String remark = noteRemarkEdit.getText().toString().trim();
		String date = noteTimeDateEdit.getText().toString().trim();
		String time = noteTimeMinuteEdit.getText().toString().trim();
		int id = NoteHelper.getInstance().insert(new Note(name, remark, date, time));
		
		AlarmManager am= (AlarmManager) getSystemService(ALARM_SERVICE);
		Intent intent = new Intent("com.heimali.sf.ALARM");
		intent.putExtra(NoteDetailActivity.INTENT_NOTE_ID, id);
		PendingIntent pi= PendingIntent.getBroadcast(this, 0, intent, 0);
		am.set(AlarmManager.RTC_WAKEUP, SystemUtils.getTimeStamp(date + " " + time + ":00"), pi);
		
		finish();
	}
	
	/**
	 * 显示日期选择器
	 */
	private void displayDatePicker() {
		int curYear = year < 0 ? SystemUtils.getCurrentYear() : year;
		int curMonth = month < 0 ? SystemUtils.getCurrentMonth()-1 : month-1;
		int curDay = day < 0 ? SystemUtils.getCurrentDay() : day;
		new DatePickerDialog(this, -1, new DatePickerDialog.OnDateSetListener() {
			@Override
			public void onDateSet(DatePicker view, int y, int m, int d) {
				year = y;
				month = m + 1;
				day = d;
				updateDateEditText();
			}
		}, curYear, curMonth, curDay).show();
	}
	
	/**
	 * 显示时间选择器 
	 */
	private void displayTimePicker() {
		new TimePickerDialog(this, -1, new TimePickerDialog.OnTimeSetListener() {
			@Override
			public void onTimeSet(TimePicker view, int h, int m) {
				hour = h;
				minute = m;
				updateTiemEidtText();
			}
		}, hour, minute, true).show();
	}
	
}
